<?php

defined('BASEPATH') or die('forbidden');

class Gmsendlog_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
    }

    function index() {
        $this->view->display("role/gmsendlog/index");
    }

   function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM {pre}admin_log WHERE {$where} and modulename like '送玩家物品'";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $this->db->query("SELECT {$colsinfo} FROM {pre}admin_log WHERE {$where} and modulename like '送玩家物品' {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default', MYSQL_ASSOC)) {
            $new_content_str ="";
            $content = $result=json_decode($n['content'],true);
            if (array_key_exists('ItemLst', $content['after'])){
                $itemLst  = $content['after']['ItemLst'];
                $new_content_str = $new_content_str . $content['after']['Value'] . ": ";
                foreach ($itemLst as $iteminfo) {
                    $new_content_str = $new_content_str . "[物品ID: " . $iteminfo['ID'] . ", 数量:" . $iteminfo['Num'] . "], ";
                }
            }else if (array_key_exists('item', $content['after'])){
                $new_content_str = $new_content_str . $content['after']['title'] . ": ";
                $names = $content['after']['item'];
                $amounts = $content['after']['amount'];
                $item_count = count($names);
                for ($i=0;$i<$item_count;$i++) {
                    $new_content_str = $new_content_str . "[ " . $names[$i] . ", " . $amounts[$i] . "], ";
                }
            }

            $n['content'] = $new_content_str;
            
            $rows[] = array('id' => $n['id'], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    //edit没用上
    function edit($id = 0) {
        if ($id === 0)
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        $row = $this->db->one("SELECT * FROM {pre}admin_log WHERE id='{$id}'");
        if (!is_array($row))
        {
            error(translate('C_INVALID_PARAM_ID', $id));
        }
        if (is_submit()) {
            $rowdata = $_POST['row'];
            //$rowdata['createtime'] = strtotime($rowdata['createtime']);
            insert_log(translate('MENU_ROLE_LOG'),translate('EDIT'), $row);
            $this->db->update("{pre}admin_log", $rowdata, array('id' => $id));
            cp_tips("success", $id);
        }
        $this->view->display("log/error/edit", array('row' => $row));
    }

    function del($id = 0) {
        $c = 0;
        $r = '';
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT * FROM {pre}admin_log WHERE id='{$id}'");
        if (!is_array($row))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        if ($this->db->delete('{pre}admin_log', array('id' => $id))) {
            insert_log(translate('MENU_ROLE_LOG'),translate('DEL'), array('id'=>$id));
            $c = 1;
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM {pre}admin_log WHERE id IN ({$ids})");
                insert_log(translate('MENU_ROLE_LOG'),translate('MULTI_DEL'), array("ids"=>$ids));
                $c = 1;
            } else if ($type == 'clearall') {
                $this->db->query("DELETE FROM {pre}admin_log");
                insert_log(translate('MENU_ROLE_LOG'),translate('ROLE_LOG_CLEARALL'), array("ids"=>$ids));
                $c = 1;
            } else if ($type == 'reject') {
                $this->db->query("UPDATE {pre}admin_log SET state='0' WHERE id IN ({$ids})");
                insert_log(translate('MENU_ROLE_LOG'),translate('UPDATE'), array("ids"=>$ids));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}